package com.bobi.tfca.network.client;

import android.util.Log;

import com.bobi.tfca.model.LoginStatus;
import com.bobi.tfca.network.response.MemberResponse;

/**
 * 内存缓存
 */
public class AppCache {
    private static final String TAG = "AppCache";
    private static PushService service;
    private static MemberResponse myInfo;

    private static LoginStatus status = LoginStatus.UNLOGIN;

    public static PushService getService() {
        return service;
    }

    public static void setService(PushService service) {
        AppCache.service = service;
    }

    public static MemberResponse getMyInfo() {
        return myInfo;
    }

    public static void setMyInfo(MemberResponse myInfo) {
        AppCache.myInfo = myInfo;
    }

    public static LoginStatus getStatus() {
        return status;
    }

    public static void updateStatus(LoginStatus newStatus) {
        if (status != newStatus) {
            Log.d(TAG, "update status from " + status + " to " + newStatus);
            status = newStatus;
        }
    }

    public static boolean checkLoginStatus() {
        return !(status.equals(LoginStatus.UNLOGIN) || status.equals(LoginStatus.LOGINING));
    }

    public static boolean checkSocketConnectStatus() {
        return status.equals(LoginStatus.CONNECTED);
    }

    public static boolean logout() {
        AppCache.updateStatus(LoginStatus.UNLOGIN);
        if (AppCache.getService() != null) {
            AppCache.getService().close();
        }
        return true;
    }
}
